package com.jby.seckill.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jby.seckill.pojo.Order;
import com.jby.seckill.pojo.SeckillOrder;
import com.jby.seckill.pojo.User;
import com.jby.seckill.service.IGoodsService;
import com.jby.seckill.service.IOrderService;
import com.jby.seckill.service.ISeckillOrderService;
import com.jby.seckill.vo.GoodsVo;
import com.jby.seckill.vo.RespBean;
import com.jby.seckill.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description 秒杀商品
 * @Author Jiang
 * @Date 2021/12/23 16:44
 * @Version 1.0.0
 */
@Controller
@RequestMapping("/seckill")
@Slf4j
public class SeckillController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private ISeckillOrderService seckillOrderService;

    @Autowired
    private IOrderService orderService;


    /**
     * 秒杀接口
     * 优化前压测：上午9：30
     * Windows优化前压测QPS：359.6
     *
     * @param user
     * @param model
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/doSeckill",method = RequestMethod.POST)
    @ResponseBody
    public RespBean doSeckill(User user, Model model, Long goodsId) {
        if (user == null) {
            return RespBean.error(RespBeanEnum.USER_NOT_EXIST);
        }

        GoodsVo goodsVo = goodsService.findGoodsVoById(goodsId);
        //判断库存
        if (goodsVo.getStockCount() < 1) {
            model.addAttribute("errmsg", RespBeanEnum.EMPTY_STOCK.getMessage());
            return RespBean.error(RespBeanEnum.EMPTY_STOCK);
        }
        //判断是否重复抢购
        SeckillOrder seckillOrder = seckillOrderService.getOne(new QueryWrapper<SeckillOrder>().eq("user_id", user.getId())
                .eq("goods_id", goodsId));

        if (seckillOrder != null) {
            model.addAttribute("errmsg",RespBeanEnum.REPEAT_ERROR.getMessage());
            return RespBean.error(RespBeanEnum.REPEAT_ERROR);
        }

        Order order = orderService.seckill(user,goodsVo);

        return RespBean.success(order);

    }


    @RequestMapping("/doSeckill2")
    public String doSeckill2(User user, Model model, Long goodsId) {
        if (user == null) {
            return "login";
        }
        model.addAttribute("user", user);

        GoodsVo goodsVo = goodsService.findGoodsVoById(goodsId);
        //判断库存
        if (goodsVo.getStockCount() < 1) {
            model.addAttribute("errmsg", RespBeanEnum.EMPTY_STOCK.getMessage());
            return "secKillFail";
        }
        //判断是否重复抢购
        SeckillOrder seckillOrder = seckillOrderService.getOne(new QueryWrapper<SeckillOrder>().eq("user_id", user.getId())
                .eq("goods_id", goodsId));

        if (seckillOrder != null) {
            model.addAttribute("errmsg",RespBeanEnum.REPEAT_ERROR.getMessage());
            return "secKillFail";
        }

        Order order = orderService.seckill(user,goodsVo);
        model.addAttribute("order",order);
        model.addAttribute("goods",goodsVo);

        return "orderDetail";

    }
}
